package com.example.star.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.star.R;
import com.example.star.contract.UserContract;
import com.example.star.entity.User;
import com.example.star.presenter.UserPresenter;

public class LoginActivity extends AppCompatActivity implements UserContract.View {
    private EditText mLoginUsername;
    private EditText mLoginPassword;
    private Button mBtnLogin;
    private TextView mRegister;
    private UserContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mLoginUsername = findViewById(R.id.login_username);
        mLoginPassword = findViewById(R.id.login_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mRegister = findViewById(R.id.register);

        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mLoginUsername.getText().toString().trim();
                String password = mLoginPassword.getText().toString().trim();

                if (username.isEmpty() || password.isEmpty()){
                    Toast.makeText(LoginActivity.this,getString(R.string.username_and_password),Toast.LENGTH_SHORT).show();
                }else {
                    User user = new User();
                    user.setUsername(username);
                    user.setPassword(password);
                    presenter.login(user);
                }
            }
        });
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

        presenter = new UserPresenter(this);
    }
    @Override
    public void showLoading() {
        // 显示加载框
    }

    @Override
    public void hideLoading() {
        // 隐藏加载框
    }

    @Override
    public void onLoginSuccess() {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        // 将用户名保存到 SharedPreferences
        String username = mLoginUsername.getText().toString().trim();
        String loginPassword = mLoginPassword.getText().toString().trim();
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", username);
        editor.putString("loginPassword", loginPassword);
        editor.apply();

        // 跳转到主界面
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onLoginFailure(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRegisterSuccess() {
        // 这里不需要处理
    }

    @Override
    public void onRegisterFailure(String message) {
        // 这里不需要处理
    }

    @Override
    public void showPasswordMismatch() {
        // 这里不需要处理
    }

    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}